package com.blb.sevice.Impl;

import com.blb.entity.User;
import com.blb.mapper.UserMapper;
import com.blb.sevice.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;

/**
 * @author lvtian
 * @date 2023/1/6 9:20
 */
@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper userMapper;

    //注入事务管理器
    @Autowired
    private DataSourceTransactionManager transactionManager;
    @Transactional
    @Override
    public void updateTwoUser() {
        //启动事务
//        TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition());
        User u1 = new User();
        u1.setId(1);
        u1.setNickname("pppppp");
        userMapper.update(u1);
//        if(1 == 1) {
//            throw new RuntimeException("你滴爷爷进化失败啦!!");
//        }
        User u2 = new User();
        u2.setId(2);
        u2.setNickname("yyyyy");
        userMapper.update(u2);

//        transactionManager.commit(status);
//        System.out.println("事务提交");
    }
}
